package com.magicsoft.share.ui.my;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.presenter.my.EditPersonalInfoPresenter;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.my.IEditPersonalInfoView;

import butterknife.BindView;
import butterknife.OnClick;

public class NickNameActivity extends BaseActivity<IEditPersonalInfoView, EditPersonalInfoPresenter> implements IEditPersonalInfoView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.edt)
    EditText edt;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String nickName;


    @Override
    public EditPersonalInfoPresenter initPresenter() {
        return new EditPersonalInfoPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nick_name;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvRight.setText("完成");
        tvTitle.setText("昵称");

        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int i = 7 - editable.length();
                tvNum.setText(i + "");
            }
        });

    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.ibtn_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                setNickName();
                break;
        }
    }

    public void setNickName() {
        nickName = edt.getText().toString().trim();
        if (nickName.equals("")) {
            ToastUtils.showToast(this, "昵称不能为空");

        } else {

            mPresenter.editUserDetail(nickName, null, null, null, null);
        }
    }


    @Override
    public void editUserDetailSuccess() {
        ToastUtils.showToast(this, "昵称设置完成");
        setResult(RESULT_OK);
        finish();
    }
}
